﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class AttachDetailDA : IAttachDetailDA
    {
        private string ATTACH_ID = "@ATTACH_ID";
        private string ATTACH_NAME = "@ATTACH_NAME";
        private string ATTACH_SIZE = "@ATTACH_SIZE";
        private string ATTACH_TYPE = "@ATTACH_TYPE";
        private string AUDIT_BY = "@AUDIT_BY";
        private string AUDIT_DATE = "@AUDIT_DATE";
        private string AUDIT_TYPE = "@AUDIT_TYPE";
        private string CREATED_BY = "@CREATED_BY";
        private string DESCR = "@DESCR";
        private string DISPLAY_NAME = "@DISPLAY_NAME";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string IS_AUDIT = "@IS_AUDIT";
        private string LINE_ID = "@LINE_ID";
        private string SQL_CHECK_ATTACH_DETAIL_ID_UNIQUE = " SELECT COUNT(1) FROM @_@ATTACH_DETAIL WHERE ATTACH_ID = @ATTACH_ID AND LINE_ID = @LINE_ID ";
        private string SQL_DELETE_ATTACH_DETAIL = " DELETE FROM @_@ATTACH_DETAIL WHERE ATTACH_ID = @ATTACH_ID ";
        private string SQL_INSERT_ATTACH_DETAIL = " INSERT INTO @_@ATTACH_DETAIL ( ATTACH_ID, LINE_ID, URL, ATTACH_NAME, DISPLAY_NAME, DESCR, STATUS, ATTACH_SIZE, ATTACH_TYPE, IS_AUDIT, AUDIT_DATE, AUDIT_BY, AUDIT_TYPE, IS_ACTIVE, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @ATTACH_ID, @LINE_ID, @URL, @ATTACH_NAME, @DISPLAY_NAME, @DESCR, @STATUS, @ATTACH_SIZE, @ATTACH_TYPE, @IS_AUDIT, @AUDIT_DATE, @AUDIT_BY, @AUDIT_TYPE, @IS_ACTIVE, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_ATTACH_DETAIL = " SELECT  ROWID, ATTACH_ID, LINE_ID, URL, ATTACH_NAME, DISPLAY_NAME, DESCR, STATUS, ATTACH_SIZE, ATTACH_TYPE, IS_AUDIT, AUDIT_DATE, AUDIT_BY, AUDIT_TYPE, IS_ACTIVE, TS, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@ATTACH_DETAIL ";
        private string SQL_UPDATE_ATTACH_DETAIL = " UPDATE @_@ATTACH_DETAIL SET URL = @URL, ATTACH_NAME = @ATTACH_NAME, DISPLAY_NAME = @DISPLAY_NAME, DESCR = @DESCR, STATUS = @STATUS, ATTACH_SIZE = @ATTACH_SIZE, ATTACH_TYPE = @ATTACH_TYPE, IS_AUDIT = @IS_AUDIT, AUDIT_DATE = @AUDIT_DATE, AUDIT_BY = @AUDIT_BY, AUDIT_TYPE = @AUDIT_TYPE, IS_ACTIVE = @IS_ACTIVE, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE ATTACH_ID = @ATTACH_ID AND LINE_ID = @LINE_ID  ";
        private string STATUS = "@STATUS";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string URL = "@URL";

        public bool CheckAttachDetailIDUnique(string attachID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachID), new SqlParameter("@LINE_ID", lineID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_ATTACH_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckAttachDetailIDUnique(DataBase dataBase, DbTransaction tran, string attachID, string lineID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachID), new SqlParameter("@LINE_ID", lineID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_ATTACH_DETAIL_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteAttachDetail(string attachDetailID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strSql = this.SQL_DELETE_ATTACH_DETAIL.Replace("@_@", whLoginID);
            try
            {
                SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachDetailID) };
                result += dataBase.ExecuteNonQuery(tran, CommandType.Text, strSql, paras);
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteAttachDetail(string attachDetailID, List<string> lineID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string strSql = this.SQL_DELETE_ATTACH_DETAIL + "AND LINE_ID = @LINE_ID ";
            try
            {
                for (int i = 0; lineID.Count > i; i++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachDetailID), new SqlParameter("@LINE_ID", lineID[i]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, strSql.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteAttachDetail(DataBase dataBase, DbTransaction tran, string attachID, List<string> lineID, string whLoginID)
        {
            int result = 0;
            string strSql = this.SQL_DELETE_ATTACH_DETAIL + "AND LINE_ID = @LINE_ID ";
            try
            {
                for (int j = 0; j < lineID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachID), new SqlParameter("@LINE_ID", lineID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, strSql.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllAttachDetail(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ATTACH_DETAIL.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllAttachDetail(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ATTACH_DETAIL.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public AttachDetailInfo GetAttachDetailByID(string attachID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ATTACH_DETAIL.Replace("@_@", whLoginID) + " WHERE ATTACH_ID = @ATTACH_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachID), new SqlParameter("@LINE_ID", lineID) };
            AttachDetailInfo attachDetailInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    DateTime CS00003;
                    attachDetailInfo = new AttachDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["ATTACH_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["URL"].ToString(), reader["ATTACH_NAME"].ToString(), reader["DISPLAY_NAME"].ToString(), reader["DESCR"].ToString(), reader["STATUS"].ToString(), (reader["ATTACH_SIZE"].ToString() != "") ? decimal.Parse(reader["ATTACH_SIZE"].ToString()) : 0M, reader["ATTACH_TYPE"].ToString(), reader["IS_AUDIT"].ToString(), (reader["AUDIT_DATE"].ToString() != "") ? DateTime.Parse(reader["AUDIT_DATE"].ToString()) : (CS00003 = new DateTime()), reader["AUDIT_BY"].ToString(), reader["AUDIT_TYPE"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return attachDetailInfo;
        }

        public AttachDetailInfo GetAttachDetailByID(DataBase dataBase, DbTransaction tran, string attachID, string lineID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ATTACH_DETAIL.Replace("@_@", whLoginID) + " WHERE ATTACH_ID = @ATTACH_ID AND LINE_ID = @LINE_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachID), new SqlParameter("@LINE_ID", lineID) };
            AttachDetailInfo attachDetailInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                DateTime CS00003;
                attachDetailInfo = new AttachDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["ATTACH_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["URL"].ToString(), reader["ATTACH_NAME"].ToString(), reader["DISPLAY_NAME"].ToString(), reader["DESCR"].ToString(), reader["STATUS"].ToString(), (reader["ATTACH_SIZE"].ToString() != "") ? decimal.Parse(reader["ATTACH_SIZE"].ToString()) : 0M, reader["ATTACH_TYPE"].ToString(), reader["IS_AUDIT"].ToString(), (reader["AUDIT_DATE"].ToString() != "") ? DateTime.Parse(reader["AUDIT_DATE"].ToString()) : (CS00003 = new DateTime()), reader["AUDIT_BY"].ToString(), reader["AUDIT_TYPE"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00003 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return attachDetailInfo;
        }

        public DataSet GetAttachDetailByQueryList(List<string> sqlWhere, AttachDetailQueryEntity attachdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_ATTACH_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!attachdetailQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, attachdetailQuery.CurrentPage, attachdetailQuery.PageSize, attachdetailQuery.SortField, attachdetailQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetAttachDetailByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, AttachDetailQueryEntity attachdetailQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_ATTACH_DETAIL.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, attachdetailQuery.CurrentPage, attachdetailQuery.PageSize, attachdetailQuery.SortField, attachdetailQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public List<AttachDetailInfo> GetAttachDetailListByID(string attachID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_ATTACH_DETAIL.Replace("@_@", whLoginID) + " WHERE ATTACH_ID = @ATTACH_ID";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@ATTACH_ID", attachID) };
            List<AttachDetailInfo> attachDetailList = new List<AttachDetailInfo>();
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                while (reader.Read())
                {
                    DateTime CS00002;
                    AttachDetailInfo attachDetailInfo = new AttachDetailInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["ATTACH_ID"].ToString(), (reader["LINE_ID"].ToString() != "") ? int.Parse(reader["LINE_ID"].ToString()) : 0, reader["URL"].ToString(), reader["ATTACH_NAME"].ToString(), reader["DISPLAY_NAME"].ToString(), reader["DESCR"].ToString(), reader["STATUS"].ToString(), (reader["ATTACH_SIZE"].ToString() != "") ? decimal.Parse(reader["ATTACH_SIZE"].ToString()) : 0M, reader["ATTACH_TYPE"].ToString(), reader["IS_AUDIT"].ToString(), (reader["AUDIT_DATE"].ToString() != "") ? DateTime.Parse(reader["AUDIT_DATE"].ToString()) : (CS00002 = new DateTime()), reader["AUDIT_BY"].ToString(), reader["AUDIT_TYPE"].ToString(), reader["IS_ACTIVE"].ToString(), (byte[]) reader["TS"], reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : (CS00002 = new DateTime()), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : (CS00002 = new DateTime()));
                    attachDetailList.Add(attachDetailInfo);
                }
            }
            return attachDetailList;
        }

        public int InsertAttachDetail(AttachDetailInfo attachDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_AttachDetail_Parameters(attachDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_ATTACH_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int InsertAttachDetail(DataBase dataBase, DbTransaction tran, AttachDetailInfo attachDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_AttachDetail_Parameters(attachDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_ATTACH_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_AttachDetail_Parameters(AttachDetailInfo attachDetailInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.ATTACH_ID, SqlDbType.VarChar, 50), new SqlParameter(this.LINE_ID, SqlDbType.Int, 4), new SqlParameter(this.URL, SqlDbType.VarChar, 100), new SqlParameter(this.ATTACH_NAME, SqlDbType.VarChar, 100), new SqlParameter(this.DISPLAY_NAME, SqlDbType.VarChar, 100), new SqlParameter(this.DESCR, SqlDbType.VarChar, 300), new SqlParameter(this.STATUS, SqlDbType.VarChar, 30), new SqlParameter(this.ATTACH_SIZE, SqlDbType.Decimal, 9), new SqlParameter(this.ATTACH_TYPE, SqlDbType.VarChar, 30), new SqlParameter(this.IS_AUDIT, SqlDbType.VarChar, 1), new SqlParameter(this.AUDIT_DATE, SqlDbType.DateTime, 8), new SqlParameter(this.AUDIT_BY, SqlDbType.VarChar, 30), new SqlParameter(this.AUDIT_TYPE, SqlDbType.VarChar, 30), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 30), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 30) };
            paramArray[0].Value = attachDetailInfo.AttachID;
            paramArray[1].Value = attachDetailInfo.LineID;
            paramArray[2].Value = attachDetailInfo.Url;
            paramArray[3].Value = attachDetailInfo.AttachName;
            paramArray[4].Value = attachDetailInfo.DisplayName;
            if (!string.IsNullOrEmpty(attachDetailInfo.Descr))
            {
                paramArray[5].Value = attachDetailInfo.Descr;
            }
            else
            {
                paramArray[5].Value = DBNull.Value;
            }
            paramArray[6].Value = attachDetailInfo.Status;
            paramArray[7].Value = attachDetailInfo.AttachSize;
            paramArray[8].Value = attachDetailInfo.AttachType;
            if (!string.IsNullOrEmpty(attachDetailInfo.IsAudit))
            {
                paramArray[9].Value = attachDetailInfo.IsAudit;
            }
            else
            {
                paramArray[9].Value = DBNull.Value;
            }
            if (attachDetailInfo.AuditDate != new DateTime())
            {
                paramArray[10].Value = attachDetailInfo.AuditDate;
            }
            else
            {
                paramArray[10].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(attachDetailInfo.AuditBy))
            {
                paramArray[11].Value = attachDetailInfo.AuditBy;
            }
            else
            {
                paramArray[11].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(attachDetailInfo.AuditType))
            {
                paramArray[12].Value = attachDetailInfo.AuditType;
            }
            else
            {
                paramArray[12].Value = DBNull.Value;
            }
            paramArray[13].Value = attachDetailInfo.IsActive;
            paramArray[14].Value = attachDetailInfo.CreatedBy;
            paramArray[15].Value = attachDetailInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateAttachDetail(AttachDetailInfo attachDetailInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                SqlParameter[] paras = this.Set_AttachDetail_Parameters(attachDetailInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_ATTACH_DETAIL.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateAttachDetail(DataBase dataBase, DbTransaction tran, AttachDetailInfo attachDetailInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                SqlParameter[] paras = this.Set_AttachDetail_Parameters(attachDetailInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_ATTACH_DETAIL.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

